package com.huang825172.sbog.Tabs.Mine.MinePage.Store;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.google.gson.Gson;
import com.huang825172.sbog.BaseFlux.Action.BaseAction;
import com.huang825172.sbog.BaseFlux.Store.BaseStore;
import com.huang825172.sbog.GlobalManager.Action.checkTokenAction;
import com.huang825172.sbog.GlobalManager.LoginStateManager;
import com.huang825172.sbog.R;
import com.huang825172.sbog.Tabs.Mine.LoginPage.Store.Datas.Login;
import com.huang825172.sbog.Tabs.Mine.MinePage.Store.Datas.userInfo;
import com.huang825172.sbog.Tabs.Mine.MinePage.Store.Datas.userInfoRes;
import com.huang825172.sbog.Util.TAGs;

import java.util.ArrayList;
import java.util.List;

import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class Store extends BaseStore {
    private LoginStateManager manager;

    private boolean tokenStatus = false;
    private boolean loginStatus = false;

    private String loginedUsername;
    private String loginedUID;

    public Store(Context context) {
        super(context);
        manager = new LoginStateManager(context);
    }

    @Override
    public List<String> getActionLabels() {
        ArrayList<String> labels = new ArrayList<>();
        labels.add("MinePageRefleshAction");
        labels.add("GlobalManagerCheckTokenAction");
        labels.add("MinePageQuitLoginAction");
        return labels;
    }

    @Override
    public void changeData(BaseAction action) {
        super.changeData(action);
        switch (action.getLabel()) {
            case "MinePageRefleshAction": {
                if (manager.getLoginToken() == null || manager.getLoginPhone() == null) {
                    loginStatus = false;
                    tokenStatus = false;
                    notifyDataChanged("MinePageStatusUnlogined");
                } else {
                    refleshUserInfo();
                }
                break;
            }

            case "GlobalManagerCheckTokenAction": {
                checkTokenAction act = (checkTokenAction) action;
                tokenStatus = act.getTokenStatus();
                notifyDataChanged(action.getLabel());
                break;
            }

            case "MinePageQuitLoginAction":{
                manager.quitLogin();
                this.notifyDataChanged(action.getLabel());
                break;
            }
            default:
                break;
        }
    }

    private void refleshUserInfo() {
        userInfo info = new userInfo(manager.getLoginToken(), manager.getLoginPhone());
        final Gson gson = new Gson();
        String infoStr = gson.toJson(info);
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(context.getResources().getString(R.string.server_baseUrl))
                .build();
        Interfaces request = retrofit.create(Interfaces.class);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), infoStr);
        Call<ResponseBody> call = request.getUserInfo(body);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String resStr = response.body().string();
                    userInfoRes resObj = gson.fromJson(resStr, userInfoRes.class);
                    if (resObj.code == 200) {
                        Log.d(TAGs.INFO, resObj.data.phone);
                        loginedUsername = resObj.data.phone;
                        loginedUID = resObj.data.id;
                        notifyDataChanged("MinePageStatusLogined");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.e(TAGs.ERROR, e.getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                t.printStackTrace();
                Log.e(TAGs.ERROR, t.getMessage());
            }
        });
    }

    public boolean getTokenStatus() {
        return tokenStatus;
    }

    public String getUsername() {
        return loginedUsername;
    }

    public String getUID() {
        return loginedUID;
    }
}
